package com.itheima.ydd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.ydd.dto.YddAdvposDto;
import com.itheima.ydd.entity.YddAdvimg;
import com.itheima.ydd.entity.YddAdvpos;
import com.itheima.ydd.mapper.YddAdvposMapper;
import com.itheima.ydd.service.IYddAdvimgService;
import com.itheima.ydd.service.IYddAdvposService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 */
@Service
public class YddAdvposServiceImpl extends ServiceImpl<YddAdvposMapper, YddAdvpos> implements IYddAdvposService {

    @Autowired
    private IYddAdvimgService yddAdvimgService;

    @Override
    public Page<YddAdvposDto> pageByCondition(Page<YddAdvpos> page, LambdaQueryWrapper<YddAdvpos> queryWrapper) {
        //分页条件查询
        this.page(page,queryWrapper);
        //数据拷贝
        Page<YddAdvposDto> dtoPage = new Page<>();
        BeanUtils.copyProperties(page,dtoPage,"recods");
        List<YddAdvposDto> list = page.getRecords().stream().map((yddAdvpos) -> {
            YddAdvposDto dto = new YddAdvposDto();
            BeanUtils.copyProperties(yddAdvpos, dto);
            LambdaQueryWrapper<YddAdvimg> yddAdvimgLambdaQueryWrapper = new LambdaQueryWrapper<>();
            yddAdvimgLambdaQueryWrapper.eq(YddAdvimg::getAdvimgpos, yddAdvpos.getId());
            int count = yddAdvimgService.count(yddAdvimgLambdaQueryWrapper);
            dto.setAdvposnum(count);
            return dto;
        }).collect(Collectors.toList());
        dtoPage.setRecords(list);
        //数据返回
        return dtoPage;
    }
}
